/**
 * 
 */
package com.mths.client.page;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SingleSelectionModel;
import com.mths.client.component.Button;
import com.mths.client.component.ComboBox;
import com.mths.client.component.GridData;
import com.mths.client.component.TabPanel;
import com.mths.client.component.TextField;
import com.mths.client.constant.PageConstant;
import com.mths.client.controller.DocumentController;
import com.mths.client.controller.DocumentControllerAsync;
import com.mths.client.dto.DocTypeDTO;
import com.mths.client.util.AsyncCall;
import com.mths.client.util.ListItem;
import com.mths.client.util.PageUtil;

/**
 * @author ibesli
 * 
 */
public class DocumentTypeCreate extends AbstractPage implements IPage {

	private static DocumentCreateDocUiBinder	uiBinder	= GWT.create(DocumentCreateDocUiBinder.class);

	interface DocumentCreateDocUiBinder extends UiBinder<Widget, DocumentTypeCreate> {
	}

	private final DocumentControllerAsync	documentAsync	= GWT.create(DocumentController.class);

	@UiField
	TabPanel								documentTabPnl;

	@UiField
	TextField								nameTxt;

	@UiField
	Button									saveBtn, clearBtn;

	@UiField
	ComboBox								docClassCmb;

	@UiField
	GridData<DocTypeDTO>					docTypeTbl;

	private DocTypeDTO						dto				= new DocTypeDTO();

	public DocumentTypeCreate() {
		initWidget(uiBinder.createAndBindUi(this));
		initEventHandler();
		initGridData();
		loadComponentData();
	}

	private void initGridData() {

		// doc typecelltable
		docTypeTbl.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

		TextColumn<DocTypeDTO> docTypeNameClm = new TextColumn<DocTypeDTO>() {

			@Override
			public String getValue(DocTypeDTO object) {
				return object.getDocTypeName();
			}
		};
		docTypeNameClm.setSortable(true);
		docTypeTbl.addColumn(docTypeNameClm, "Döküman Class Adı");

		final SingleSelectionModel<DocTypeDTO> selectionModelDocType = new SingleSelectionModel<DocTypeDTO>();
		docTypeTbl.setSelectionModel(selectionModelDocType);
		docTypeTbl.addDomHandler(new DoubleClickHandler() {

			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				dto = selectionModelDocType.getSelectedObject();
				preparePageUpdate();
			}

		}, DoubleClickEvent.getType());

	}

	private void initEventHandler() {
		documentTabPnl.selectTab(3);
		documentTabPnl.addSelectionHandler(new SelectionHandler<Integer>() {

			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				if (event.getSelectedItem() == 0) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_META_CREATE);
				} else if (event.getSelectedItem() == 1) {
					// loadDocumentType()
				} else if (event.getSelectedItem() == 2) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_CLASS_CREATE);
				} else if (event.getSelectedItem() == 3) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_TYPE_CREATE);
				} else if (event.getSelectedItem() == 4) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_MATCH_CREATE);
				}
			}
		});

		clearBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				refreshPage();
			}
		});

		saveBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				saveOrUpdateDocType();
			}
		});

		docClassCmb.addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				refreshPage();
				loadDocTypeTable();
			}

		});

	}

	private void loadDocTypeTable() {
		documentAsync.loadDocTypeList(docClassCmb.getFieldValue(), new AsyncCall<List<DocTypeDTO>>() {

			@Override
			public void successCall(List<DocTypeDTO> result) {
				docTypeTbl.putDataList(result);
			}
		});
	}

	private void preparePageUpdate() {
		nameTxt.setFieldValue(dto.getDocTypeName());
	}

	private void loadComponentData() {
		loadDocClassCmb();
	}

	private void loadDocClassCmb() {
		documentAsync.loadDocClassCombo("", new AsyncCall<List<ListItem>>() {

			@Override
			public void successCall(List<ListItem> result) {
				docClassCmb.addItemList(result);
			}

		});
	}

	private void saveOrUpdateDocType() {
		dto.setDocTypeName(nameTxt.getFieldValue());
		dto.setDocClassOid(docClassCmb.getFieldValue());
		documentAsync.saveDocType(dto, new AsyncCall<Void>() {

			@Override
			public void successCall(Void result) {
				refreshPage();
				loadDocTypeTable();
			}

		});
	}

	private void refreshPage() {
		nameTxt.setFieldValue("");
		docTypeTbl.clear();
		dto = new DocTypeDTO();
	}

	@Override
	public String pageName() {
		return messages.DocumentTypeCreate_pageName();
	}
}
